import gtk
from os import path

class TrayIcon(gtk.StatusIcon):


  def __init__(self, parent):
    self._parent = parent

    self._init_menu()
    self._init_icon()

  def _init_icon(self):
    gtk.StatusIcon.__init__(self)
    self.set_from_file(path.join(path.dirname(__file__), 'tray.png'))
    self.connect("popup-menu", self._on_right_click, self._menu)
    self.connect("activate", self.toggle_window)
    self.set_tooltip(self._parent.__project__)
    self.set_visible(True)

  def toggle_window(self, wdgt):
    self._parent.toggle_window()

  def _on_right_click(self, icon, btn, time, menu):
    menu.show_all()
    menu.popup(None, None, None, btn, time)

  def _init_menu(self):
    self._menu = gtk.Menu()

    exit = gtk.ImageMenuItem(gtk.STOCK_QUIT)
    exit.connect("activate", self.exit_app)
    self._menu.append(exit)

  def exit_app(self, obj):
    gtk.main_quit()